package com.net.xpay.core.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayEbppIndustryJobPayslipSyncModel;
import com.alipay.api.request.AlipayEbppIndustryJobPayslipSyncRequest;
import com.alipay.api.response.AlipayEbppIndustryJobPayslipSyncResponse;
import com.net.xpay.alipay.AlipayProperties;
import com.net.xpay.alipay.enums.ChannelType;
import com.net.xpay.core.service.AlipayEbppIndustryJobPayslipSync;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;
@Slf4j
@Service
public class AlipayEbppIndustryJobPayslipSyncImpl implements AlipayEbppIndustryJobPayslipSync {
    @Autowired
    private AlipayProperties alipayProperties;
    @Override
    public void payslipSync(AlipayEbppIndustryJobPayslipSyncModel model) throws Exception{
        String privateKey  = alipayProperties.getAppPayslipPrivateKey();
        String alipayPublicKey = alipayProperties.getAlipayPayslipPublicKey();
        String appId = alipayProperties.getAlipayPayslipAppId();
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        // 构造请求参数以调用接口
        AlipayEbppIndustryJobPayslipSyncRequest request = new AlipayEbppIndustryJobPayslipSyncRequest();
        if(ChannelType.BANK_CARD.getValue().equals(model.getChannel())){
            model.setBankCode("COMMON_BANK");//默认公共银行
        }
        request.setBizModel(model);
        AlipayEbppIndustryJobPayslipSyncResponse response = alipayClient.execute(request);
        log.info("PayslipSync:"+response.getBody()+"\n调用结果:"+response.isSuccess());
    }


    public static void main(String[] args) throws AlipayApiException, ParseException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        // 构造请求参数以调用接口
        AlipayEbppIndustryJobPayslipSyncRequest request = new AlipayEbppIndustryJobPayslipSyncRequest();
        AlipayEbppIndustryJobPayslipSyncModel model = new AlipayEbppIndustryJobPayslipSyncModel();

        // uid参数未来计划废弃，存量商户可继续使用，新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。
//         model.setUserId("20880003050001");
//         model.setUserId("2088122090302063");//2088122090302063

        // 设置支付宝openid
//        model.setOpenId("074a1CcTG1LelxKe4xQC0zgNdId0nxi95b5lsNpazWYoCo5");

        // 设置支付宝登录号
        model.setLoginId("15207400131");

        // 设置外部业务编号
        model.setOutBizNo("871123s934lskg1l232432se21");

        // 设置付款企业名称
        model.setCompanyName("测试公司");

        // 设置企业信用代码
//        model.setCompanyCertNo("9143032134469179X1");

        // 设置付款金额
        model.setAmount("100");

        // 设置付款时间
        model.setSalaryTime(new Date());

        // 设置用户收款账号
//        model.setCardNo("5976");

        // 设置用户收款渠道
        model.setChannel("ALIPAY");

        // 设置客服电话
//        model.setContactInfo("0571-000000");

        // 设置账单详情地址
//        model.setBillDetailUrl("alipays://xxxxx");

        // 设置付款银行简称
//        model.setBankCode("CMB");
        // 付款银行卡编码，当付款渠道为银行卡时，该字段必填
        model.setBankCode("COMMON_BANK");

        // 设置支付宝转账订单号
//        model.setOrderId("20250305020070011520800009784451");
        model.setOrderId("20250401020070011570060010635618");

        // 设置备注信息
        model.setRemark("测试日结工资");

        // 设置用户姓名
        model.setUserName("张程");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayEbppIndustryJobPayslipSyncResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCB9U5IzDnsj5AuJmqxW6HxjbNBVjfvOwzaH+E2sykMxzCGfSztjPmfkEztqw7OM6IkMN0AcvTMl+RO88I6KdPyAZEKHNlsenZHWeMvxTRCeBOgPzONyRUzKVxT0WdCOM/yvPyVml1hdbheT+5Jgdd/zoYDujBenLtHib6G6KBdf/tEJr0xQ2zaYl8uzQlj90L0ktAe6QZVrl3cTHCtZqyBAYRlPKhXHBE6QEVBLQVvIUX5uaomIyivbxnnI2y4gGI97sDEG5CVEQL5ds90EEamto/AC2yGAW6wFbRAE/xMBEJDSwwoTiohH1Sc+tIazh7KGFR268ONU7ynx9wIfWjpAgMBAAECggEAU1SfMPC7R6O6mM0LZtRR4BizU3bcgOrFT+xXNgLllIr445PKRjw6KAWYPmGMKc5T7o7idxI/RB01SSbmaPM/MxOsE+bxx6wywYeytIPoAaMLOpqsDQNeM6MWz6wbYQwa0xQ5hhgoPoEEin0IV1wJeatMpYEy2Mf5EmMRp2HsymaClGYHMBZi4pRTQzBq8q4lIEKw4KWNdHSH9vliz2s8ub2Kw0Sn2bFJEV4A2YhxPbBnqG9K3bqlIOS5ce7+aqBm58BShfx4YaDor9+SZ0/jI/BJklMlsnZwoP3VG4mDYyV5YKhHwZYPTgUfLYc9cLPIaxU9LuH+xRUNvt9EQfuYMQKBgQDeMCq6wzGZ1/7p3TysrOCGBrbm83Ke1bZ9U3kueRscUgT0UeQO1kWlkU43aZGgUWiXvKLT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DmN1+D41BzVQr7DS7w97U4frVW4gJ3p418yd3ZcndD365Ae7I2aIsyX2CTMRUX96ZsU+gvTU3XgdFnKxGma2IAoec8ALGZGQ2WpkHuft4/7vh3mdUsFvLI7uQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021005141626476");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

}